import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import schedule from '../utils/schedule'
import match from '../utils/match'
Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        //公共组件
        TimeLine_active: [false, false, false, false, false],
        TimeLineArr: [],
        // 编排
        matchLayout: {},
        com_id: 1,
        AddMatchDetailStep: 1,
        UserInfo: {},
        MatchList: [],
        MatchListAll: [],
        MatchCheck: {
            ref_id: {
                get_user: ""
            }
        },
        MatchCheckInputs: {},
        AddMatchDetailObj: {},
        //签到
        MatchSign: {
            name: '',
            get_ref: {
                get_user: {
                    name: ''
                }
            }
        },
        MatchSignUpTable: [],
        MatchSignUpLists: [],
        checkMatchSignUp: {}, //核对签到
        //编排
        MatchInfoLayout: [],
        LayoutResultTypeId: 0,
        LayoutKnockResult: [],
        LayoutLoopResult: [],
        //排战
        platoonList: {},
        platoonMatch: {},
        platoonInfo: {},
        platoonGrounds: [],
        platoonName: [],
        roundsSelected: [],
        teamsInfo: [],
        secondRounds: [],
        firstRounds: [],
        platoonDate: '',
        projectName: [],
        MatchStatus: {},
        rankList: [] //成绩显示
    },
    mutations: {
        //公共组件
        setTimeLine_active(state, arr) {
            state.TimeLine_active = arr
        },
        setTimeLineArr(state, arr) {
            state.TimeLineArr = arr
        },
        setMatchStatus(state, setMatchStatus) {
            state.MatchStatus = setMatchStatus
        },
        setMatchStatusList(state, setMatchStatusList) {
            state.MatchSignUpLists = setMatchStatusList
        },
        setMatchInfo(state, matchInfo) {
            state.matchLayout = matchInfo
        },
        setAddMatchDetailStep(state, step) {
            state.AddMatchDetailStep = step
        },
        setAddMatchDetailObj(state, obj) {
            state.AddMatchDetailObj = obj
        },
        // 获取项目组别必填信息
        setMatchSignUpProp(state, porpList) {
            state.projectName = porpList
        },


        setUserInfo(state, info) {
            state.UserInfo = info
        },
        setMatchListAll(state, list) {
            state.MatchListAll = list
        },
        setMatchList(state, list) {
            state.MatchList = list
        },
        setMatchCheck(state, check) {
            state.MatchCheck = check
        },
        setMatchSign(state, signobj) {
            state.MatchSign = signobj
        },
        setMatchSignUpTable(state, obj) {
            state.MatchSignUpTable = obj
        },
        setMatchCheckInputs(state, obj) {
            state.MatchCheckInputs = obj
        },
        setMatchInfoLayout(state, arr) {
            state.MatchInfoLayout = arr
        },
        //核对签到信息
        setCheckMatchSignUp(state, obj) {
            state.checkMatchSignUp = obj
        },
        //编排
        setLayoutResultTypeId(state, id) {
            state.LayoutResultTypeId = id
        },
        setLayoutResult(state, obj) {
            if (state.LayoutResultTypeId == 3) {
                state.LayoutKnockResult = obj[0][0]
            }
            if (state.LayoutResultTypeId == 1 || state.LayoutResultTypeId == 2 || state.LayoutResultTypeId == 4) {
                state.LayoutLoopResult = obj
            }
            if (state.LayoutResultTypeId == 5) {
                state.LayoutKnockResult = obj[0]
            }
        },
        //排战
        //战表展示
        setPlatoonList(state, obj) {
            var aa = JSON.stringify(obj);
            localStorage.setItem("data", aa);
            state.platoonList = obj
        },
        //
        setPlatoonMatch(state, obj) {
            state.platoonMatch = obj
        },
        setPlatoonInfo(state, obj) {
            state.platoonInfo = obj
        },
        setPlatoonGrounds(state, arr) {
            state.platoonGrounds = arr
        },
        setPlatoonName(state, arr) {
            state.platoonName = arr
        },
        setRoundsSelected(state, arr) {
            state.roundsSelected = arr
        },
        setTeamsInfo(state, arr) {
            state.teamsInfo = arr
        },
        setSecondRounds(state, arr) {
            state.secondRounds = arr
        },
        setFirstRounds(state, arr) {
            state.firstRounds = arr
        },
        setPlatoonDate(state, date) {
            state.platoonDate = date
        },
        //战表显示
        setRankList(state, arr) {
            state.rankList = arr
        },
    },
    actions: {
        //公共组件 timeLine
        getTimeLine(context, com_id) {
            return new Promise((resolve, reject) => {
                axios.post('/TimeLine/time_line', {
                    com_id: com_id
                }, {
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                }).then((res) => {
                    if (res.data.code == 2000) {
                        let TimeLineArr = []
                        let TimeLine_active = [false, false, false, false, false] //重置
                        let timeArr = match.setTimeLineRouter(res.data.data) //增加时间轴路由
                        TimeLineArr = JSON.parse(JSON.stringify(timeArr))
                            //导航 最后一个全为false时，删除
                        for (let lastIndex = timeArr.length - 1; lastIndex > 0; lastIndex--) {
                            let count = 0
                            for (let step in timeArr[lastIndex]) {
                                if (timeArr[lastIndex][step].status) count = 1
                            }
                            count == 0 ? timeArr.splice(timeArr.length - 1, 1) : ''
                            if (timeArr.length - 1 == lastIndex) {
                                break;
                            }
                        }
                        for (let a in timeArr) {
                            TimeLine_active[a] = true
                        }
                        context.commit('setTimeLine_active', TimeLine_active)
                        context.commit('setTimeLineArr', TimeLineArr)
                    } else {
                        this.$message({
                            type: 'error',
                            message: '获取时间轴失败'
                        })
                    }
                    resolve() // 感觉无需失败回调 只需要回调
                })
            })
        },
        getMatchList(context, obj) {
            axios
                .post(
                    '/event/com_list', {
                        name: obj.name,
                        type: obj.type,
                        page: obj.currentPage
                    }, {
                        headers: {
                            token: window.sessionStorage.getItem('token')
                        }
                    }
                )
                .then(res => {
                    context.commit('setMatchList', res.data.data)
                })
                .catch(error => {});
        },
        getMatchCheck(context, id) {
            axios.get('/event/com_info', {
                    params: {
                        com_id: id
                    },
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                })
                .then(function(res) {
                    for (let i = 0; i < res.data.data.referee.length; i++) {
                        res.data.data.referee[i].key = i + 1
                    }
                    context.commit('setMatchCheck', res.data.data)
                    var PI = res.data.data.com_score[0] //pointsInputs
                    var PIArray = []
                    for (let a in PI) {
                        if (a.substring(0, 7) == 'ranking') {
                            PIArray.push(PI[a])
                        }
                    }
                    var PIObecjt = {}
                    PIObecjt.id = parseInt(id)
                    PIObecjt.PA = PIArray
                    context.commit('setMatchCheckInputs', PIObecjt)
                })
        },
        getMatchApplySignChange(context, idObj) {
            return new Promise((resolve, reject) => {
                axios.post('/event/sign_search', {
                        com_id: idObj.com_id,
                    }, {
                        headers: {
                            token: window.sessionStorage.getItem('token')
                        }
                    })
                    .then(function(res) {
                        var obj = res.data.data
                        context.commit('setMatchSign', obj) //签到信息
                        axios.post('/event/check_sign', {
                                com_id: idObj.com_id,
                                project_id: idObj.project_id,
                            }, {
                                headers: {
                                    token: window.sessionStorage.getItem('token')
                                }
                            })
                            .then(function(res) {
                                let setMatchStatus = {
                                    com_status: res.data.data.com_status,
                                    sign_count: res.data.data.sign_count,
                                    count: res.data.data.count,
                                    project_id: res.data.data.project_id
                                }
                                context.commit("setMatchStatus", setMatchStatus)
                                let list = res.data.data.list
                                context.commit("setMatchSignUpTable", list)
                                for (let item in list) {
                                    list[item].radio = false
                                    if (list[item].status == 1) {
                                        list[item].radio = true
                                    }
                                }
                                let listArr = []
                                for (let i = 0; i < res.data.data.new_list.require.length; i++) {
                                    if (res.data.data.new_list.require[i] == '' || res.data.data.new_list.require[i] == null ||
                                        typeof(res.data.data.new_list.require[i]) == undefined) {
                                        res.data.data.new_list.require.splice(i, 1);
                                        i = i - 1;
                                    }
                                }
                                let newListArr = res.data.data.new_list.require
                                let listProp = ["name", "age", "sexs", "card", "project", "phone", "customize[0]",
                                    "customize[1]", "customize[2]"
                                ]
                                for (let i = 6; i < newListArr.length; i++) {
                                    let listProject = {
                                        prop: listProp[i],
                                        label: newListArr[i]
                                    }
                                    listArr.push(listProject)
                                }
                                context.commit('setMatchSignUpProp', listArr) //count 未知
                                resolve()
                            })
                    })
            })
        },
        //编排
        getMatchInfoLayout(context, matchId) {
            return new Promise((resolve, reject) => {
                axios.get('/competition/getCompeInfo', {
                    params: {
                        com_id: matchId
                    },
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                }).then((res) => {
                    let arr = res.data.data
                    context.commit('setMatchInfoLayout', arr)
                    resolve()
                })
            })
        },
        //战表信息
        getPlatoonInfo(context, obj) {
            if (obj.checkSchedule == 1) {
                return new Promise((resolve, reject) => {
                    axios.get('/competition/getCompeInfo', {
                        params: {
                            com_id: obj.com_id,
                        },
                        headers: {
                            token: window.sessionStorage.getItem('token')
                        }
                    }).then((res) => {
                        let PlatoonInfo = []
                        let teams = res.data.data
                        let Info = []
                        let firstRounds = 0
                        let secondRounds = 0
                        let roundsSelected = [
                            [],
                            []
                        ]
                        let firstRoundsList = []
                        let secondRoundsList = []
                            //赛事
                        context.commit('setPlatoonMatch', res.data.data)
                            //场地
                        let arr = []
                        for (var i = 0; i < res.data.data.ground_name.length; i++) {
                            arr.push({
                                "ground_name": res.data.data.ground_name[i],
                                "checked": true
                            })
                        }
                        // context.commit('setPlatoonName', res.data.data.ground_name)
                        context.commit('setPlatoonName', arr)
                        let grounds = []
                        for (let i = 0; i < res.data.data.ground_name.length; i++) {
                            grounds.push([])
                            for (let j = 0; j < res.data.data.ground_name.length; j++) {
                                grounds[i].push({
                                    id: j,
                                    list: [],
                                    "checked": true
                                })
                            }
                        }
                        context.commit('setPlatoonGrounds', grounds)
                        resolve()
                    })
                })
            }
            return new Promise((resolve, reject) => {
                axios.get('/competition/getCompeInfo', {
                    params: {
                        com_id: obj.com_id,
                    },
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                }).then((res) => {
                    let PlatoonInfo = []
                    let teams = res.data.data
                    let Info = []
                    let firstRounds = 0
                    let secondRounds = 0
                    let roundsSelected = [
                        [],
                        []
                    ]
                    let firstRoundsList = []
                    let secondRoundsList = []
                        //赛事
                    context.commit('setPlatoonMatch', res.data.data)
                        //场地
                    let arr = []
                    for (var i = 0; i < res.data.data.ground_name.length; i++) {
                        arr.push({
                            "ground_name": res.data.data.ground_name[i],
                            "checked": true
                        })
                    }
                    // context.commit('setPlatoonName', res.data.data.ground_name)
                    context.commit('setPlatoonName', arr)
                    let grounds = []
                    for (let i = 0; i < res.data.data.ground_name.length; i++) {
                        grounds.push([])
                        for (let j = 0; j < res.data.data.ground_name.length; j++) {
                            grounds[i].push({
                                id: j,
                                list: [],
                                "checked": true
                            })
                        }
                    }
                    context.commit('setPlatoonGrounds', grounds)
                        //循环promise  可以用promise.all
                        // promiseArr.push( schedule.getPlatoonManual(teams[a],firstRounds,secondRounds,firstRoundsList,secondRoundsList,Info,roundsSelected) )
                        // Promise.all(promiseArr).then(()=>{})

                    let getManualResult = async function() {
                        for (let a in teams) {
                            if (typeof teams[a] == 'object') {
                                if (teams[a].signUp_num) {
                                    PlatoonInfo.push(teams[a])
                                        //是否是查看战表页面
                                    if (obj.isCheckSchedule == true) break
                                        //是否发布签表
                                    if (teams[a].check_signs == 1) {
                                        await schedule.getPlatoonManual(teams[a], firstRounds, secondRounds,
                                            firstRoundsList, secondRoundsList, Info, roundsSelected)
                                    }
                                }
                            }
                        }
                    }
                    getManualResult().then(() => {
                            context.commit('setPlatoonInfo', PlatoonInfo)
                            context.commit('setRoundsSelected', roundsSelected)
                            context.commit('setTeamsInfo', Info)
                            context.commit('setSecondRounds', secondRoundsList)
                            context.commit('setFirstRounds', firstRoundsList)
                            resolve()
                        })
                        .catch((e) => {
                            resolve()
                        })
                })
            })
        },
        //战表展示
        getPlatoonList(context, obj) {
            return new Promise((resolve, reject) => {
                axios.post('/Platoon/index', {
                    com_id: obj.com_id,
                    date: obj.date
                }, {
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                }).then((res) => {
                    if (res.data.code == 200) {
                        context.commit('setPlatoonList', res.data.data)
                        resolve()
                    } else {
                        reject(res.data.msg)
                    }
                })
            })
        },
        //成绩展示
        getRankList(context, obj) {
            return new Promise((resolve, reject) => {
                axios.post('/Platoon/rank_list', {
                    com_id: obj.com_id,
                    group_id: obj.project_id,
                }, {
                    headers: {
                        token: window.sessionStorage.getItem('token')
                    }
                }).then((res) => {
                    if (res.data.code == 200) {
                        let arr = []
                        for (let item in res.data.data) {
                            if (typeof res.data.data[item] == 'object' && res.data.data[item] != null && res.data.data[
                                    item] != undefined) {
                                res.data.data[item].com_system = res.data.data.com_system ? res.data.data.com_system :
                                    false
                                arr.push(res.data.data[item])
                            }
                        }
                        context.commit('setRankList', arr)
                        resolve()
                    } else {
                        context.commit('setRankList', res.data.data)
                        reject(res)
                    }
                })
            })
        },
    }
})
